Skip to content

Register the Turbo Stream test encoder once - #760

Merged
brunoprietog merged 2 commits into
hotwired:mainfrom
viralpraxis:redefine-accept-header-method
Nov 22, 2026
Merged

brunoprietog merged 2 commits into
hotwired:mainfrom
viralpraxis:redefine-accept-header-method

Conversation

@viralpraxis

@viralpraxis viralpraxis commented Nov 15, 2025 •

Copy link
Copy Markdown
Contributor

Since the action_dispatch_integration_test hook is invoked for each integration test, the whole TurboStreamEncoder is (re)defined many times.

Ideally, I think, @encoders[:turbo_stream] should be set once, but since RequestEncoder does not expose straightforward API to check if an encoder is registered, we'd have to use something like

ActionDispatch::RequestEncoder::IdentityEncoder === ActionDispatch::RequestEncoder.encoder(:turbo_stream)

which is not handy.

NB: requiring AS is not necessary -- we can use the self-alias trick instead

Edit:

Register the shared Turbo Stream test encoder once using Active Support’s run_once option. This prevents accept_header redefinition warnings when the integration test hook runs multiple times.

Since the `action_dispatch_integration_test` hook is invoked
for each integration test, the whole `TurboStreamEncoder` is (re)defined
many times.

Ideally, I think, `@encoders[:turbo_stream]` should be set once,
but since `RequestEncoder` does not expose straightforward API to check
if an encoder is registered, we'd have to use something like

```ruby
ActionDispatch::RequestEncoder::IdentityEncoder === ActionDispatch::RequestEncoder.encoder(:turbo_stream)
```

which is not handy.

NB: requiring AS is not necessary -- we can use the self-alias trick instead
@viralpraxis

Copy link
Copy Markdown
Contributor Author

any thoughts on this? using method redefinition is also safer since it ensures that redefined method actually exists (that's not the case right now)

Comment thread lib/turbo/engine.rb Outdated
class TurboStreamEncoder < IdentityEncoder
header = [ Mime[:turbo_stream], Mime[:html] ].join(",")
define_method(:accept_header) { header }
redefine_method(:accept_header) { header }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PORO alternative (https://bugs.ruby-lang.org/issues/13574):

alias accept_header accept_header
define_method(:accept_header) { header }

@brunoprietog brunoprietog changed the title TurboStreamEncoder: redefine accept_header method Register the Turbo Stream test encoder once Nov 22, 2026

@brunoprietog brunoprietog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @viralpraxis!

@brunoprietog
brunoprietog merged commit f4696e0 into hotwired:main Nov 22, 2026
5 checks passed
@viralpraxis
viralpraxis deleted the redefine-accept-header-method branch November 23, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants